package org.MobileSales.DataManagement;

import java.util.Date;

import org.MobileSales.Model.Clients.ClientGroup;
import org.MobileSales.Model.Employees.Message;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class EmployeeManager {
	public Context context;
	
	public EmployeeManager(Context c)
	{
		this.context = c;
	}
	
	public Message GetMessage(int messageId)
	{
    	final DatabaseHelper dbHelper = new DatabaseHelper(this.context);
     	try {		
     		dbHelper.openDatabase();
     		SQLiteDatabase db = dbHelper.getDatabase();
     		
     		String query = "SELECT _id, ReceiverId, SenderId, Subject, Content, SendDate, ReceiveDate, IsNew" 
							+ " FROM Messages" 
							+ " WHERE _id = " + messageId;
     		Cursor c = db.rawQuery(query, null);
     		

     		if (!c.moveToFirst())
     			return null;
     		
     		Date now = new Date();
     		Message result = new Message();
     		result.Id = c.getInt(c.getColumnIndex("_id"));
     		result.RecepientId= c.getInt(c.getColumnIndex("ReceiverId"));
     		result.SenderId = c.getInt(c.getColumnIndex("SenderId"));
     		result.Subject = c.getString(c.getColumnIndex("Subject"));
     		result.Content = c.getString(c.getColumnIndex("Content"));
     		result.SendDate = c.getString(c.getColumnIndex("SendDate"));
     		result.ReceiveDate = now.toString();
     		result.IsNew = false;
     		
     		ContentValues cv = new ContentValues();
     		cv.put("ReceiveDate", now.toString());
     		cv.put("IsNew", "false");
     		
     		db.update("Messages", cv, "_id = "+result.Id, null);
     		    	    		  		
     		return result;
     	}
     	catch(SQLException sqle) {
     		Log.d(this.getClass().getName() ,sqle.getMessage());
     		throw sqle;
     	}catch (Exception e){
     		Log.d(this.getClass().getName(), e.getMessage());
     	}finally{
     		dbHelper.close();
     	}
     	
		return null;
	}
	public void StoreMessage(Message message)
	{
		final DatabaseHelper dbHelper = new DatabaseHelper(this.context);
     	try {
			ContentValues cv = new ContentValues();
			
			cv.put("ReceiverId",message.RecepientId);
			cv.put("SenderId", message.SenderId);
			cv.put("Subject", message.Subject);
			cv.put("Content", message.Content);
			cv.put("SendDate", message.SendDate);
			cv.put("ReceiveDate", message.ReceiveDate);
			cv.put("IsNew", message.IsNew);
			
			SQLiteDatabase db = dbHelper.getDatabase();
			if (message.Id != 0)
				db.update("Messages", cv, "_id ="+message.Id, null);
			else
				db.insert("Messages", null, cv);
     	}
     	catch(SQLException sqle) {
     		Log.d(this.getClass().getName() ,sqle.getMessage());
     		throw sqle;
     	}catch (Exception e){
     		Log.d(this.getClass().getName(), e.getMessage());
     	}
     	finally {
     		dbHelper.close();
     	}
	}
	
	public void RemoveMessage(int messageId)
	{
		final DatabaseHelper dbHelper = new DatabaseHelper(this.context);
     	try {
			SQLiteDatabase db = dbHelper.getDatabase();
			db.delete("Messages", "_id ="+messageId, null);
     	}
     	catch(SQLException sqle) {
     		Log.d(this.getClass().getName() ,sqle.getMessage());
     		throw sqle;
     	}catch (Exception e){
     		Log.d(this.getClass().getName(), e.getMessage());
     	}
     	finally {
     		dbHelper.close();
     	}
	}
}
